﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 标准事件模型服务
    /// </summary>
    public class EventModelDictService : CrudAppService<EventModelDict, EventModelDictDto, Guid, EventModelDictInput, EventModelDictCreateDto, EventModelDictUpdateDto>
    {
        /// <summary>
        /// 
        /// </summary>
        private IRepository<EventModelDict, Guid> _StdModelConfigRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="StdModelConfigRepository"></param>
        public EventModelDictService(IRepository<EventModelDict, Guid> StdModelConfigRepository) : base(StdModelConfigRepository)
        {
            _StdModelConfigRepository = StdModelConfigRepository;
        }

        /// <summary>
        /// 获取标准事件模型列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<EventModelDictDto>> GetListAsync(EventModelDictInput input)
        {
            var iQuery = await _StdModelConfigRepository.GetQueryableAsync();
            //iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), i => i.ModelName.Contains(input.Filter) || i.ModelCode.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<EventModelDict>, List<EventModelDictDto>>(iQuery.ToList());

                return new PagedResultDto<EventModelDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<EventModelDict>, List<EventModelDictDto>>(items.ToList());

                return new PagedResultDto<EventModelDictDto>(totalCount, itemsDto);
            }
        }
    }
}
